package com.qinglei.recoup.system.controller;

import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.exception.RecoupException;
import com.qinglei.recoup.common.exception.RedisConnectException;
import com.qinglei.recoup.system.manager.BreathHeartAlarmV4;
import com.qinglei.recoup.system.pojo.BedRemarksParam;
import com.qinglei.recoup.system.pojo.MonitorInfoVO;
import com.qinglei.recoup.system.pojo.BedMonitorTimeParam;
import com.qinglei.recoup.system.pojo.MonitorTimeUpdateParam;
import com.qinglei.recoup.system.pojo.OnOffMonitorParam;
import com.qinglei.recoup.system.service.BedMonitorTimeService;
import com.qinglei.recoup.system.service.BedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Api(tags = "监测相关控制器-张-已完成")
@Slf4j
@Validated
@RestController
@RequestMapping("monitor")
public class MonitorController {
    @Resource
    private BedService bedService;

    @Autowired
    private BedMonitorTimeService bedMonitorTimeService;

    @Autowired
    private BreathHeartAlarmV4 breathHeartAlarmV4;

    @ApiOperation(value = "护工实时监控页面 开关监测仪")
    @PutMapping("/onOff")
    public CommonRes onOffMonitor(@RequestBody @Valid OnOffMonitorParam param) {
        log.info("onOffMonitor param:{}", param);
        CommonRes commonRes = bedService.onOffMonitor(param);
        log.info("onOffMonitor result:{}", commonRes);
        breathHeartAlarmV4.clearAlearm(param.getBedId());
        return commonRes;
    }

    @ApiOperation(value = "护工实时监控页面 检测仪关闭备注")
    @PostMapping("/closeRemark")
    public CommonRes radarCloseRemark(@RequestBody BedRemarksParam bedRemarksParam) {
        log.info("radarCloseRemark in param:{}", bedRemarksParam);
        CommonRes commonRes = bedService.radarCloseRemark(bedRemarksParam);
        log.info("radarCloseRemark result:{}", commonRes);
        return commonRes;
    }

    @Deprecated
    @ApiOperation(value = "护工实时监控页面 添加监控时间（添加成功返回ID)[2.0废弃]")
    @PostMapping("/time")
    public CommonRes<Integer> addMonitorTime(@RequestBody @Valid BedMonitorTimeParam param) throws RedisConnectException {
        log.info("addMonitorTime param:{}", param);
        Integer res = null;
        try {
            res = bedMonitorTimeService.addBedMonitorTime(param);
        } catch (RecoupException e) {
            return CommonRes.failed(e.getMessage());
        }
        catch (Exception e) {
            log.error("addMonitorTime error", e);
            return CommonRes.failed(e.getMessage());
        }
        log.info("addMonitorTime result:{}", res);
        return CommonRes.ok(res);
    }

    @Deprecated
    @ApiOperation(value = "护工实时监控页面 删除监控时间[2.0废弃]")
    @ApiImplicitParam(name = "id", value = "监测时间ID", paramType = "path", dataType = "Integer", required = true)
    @DeleteMapping("/time/{id}")
    public CommonRes delMonitorTime(@PathVariable @NotNull(message = "{required}") Integer id) throws RedisConnectException {
        log.info("delMonitorTime param:{}", id);
        try {
            bedMonitorTimeService.delBedMonitorTime(id);
        } catch (Exception e) {
            log.error("delMonitorTime error", e);
            return CommonRes.failed(e.getMessage());
        }
        log.info("delMonitorTime result:{}", "ok");
        return CommonRes.ok();
    }

    @Deprecated
    @ApiOperation(value = "护工实时监控页面 修改监控时间[2.0废弃]")
    @PutMapping("/time")
    public CommonRes<Integer> updateMonitorTime(@RequestBody @Valid MonitorTimeUpdateParam param) throws RedisConnectException {
        log.info("updateMonitorTime param:{}", param);
        try {
            bedMonitorTimeService.updateBedMonitorTime(param);
        } catch (RecoupException e) {
            return CommonRes.failed(e.getMessage());
        }
        catch (Exception e) {
            log.error("updateMonitorTime error", e);
            return CommonRes.failed(e.getMessage());
        }
        log.info("updateMonitorTime result:{}", "ok");
        return CommonRes.ok();
    }

    @ApiOperation(value = "护工端和院长端 实时监控页面 获取监控基本信息（返回开关状态和监测时间段信息）")
    @ApiImplicitParam(name = "bedId", value = "床位ID", paramType = "path", dataType = "Integer", required = true)
    @GetMapping("/{bedId}")
    public CommonRes<MonitorInfoVO> getMonitorInfo(@PathVariable @NotNull(message = "{required}") Integer bedId) {
        log.info("getMonitorInfo param:{}", bedId);
        CommonRes<MonitorInfoVO> monitorInfo = bedService.getMonitorInfo(bedId);
        log.info("getMonitorInfo result:{}", monitorInfo);
        return monitorInfo;
    }
}
